﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using EnterpriseDT.Net.Ftp;

namespace PicturePage
{
    public partial class EditPage : Form
    {
        private Configuration config = new Configuration();
        private FTPConnection ftpConnection = new FTPConnection();

        public EditPage()
        {
            LoadPage();
        }

        public EditPage(Configuration configuration)
        {
            config = configuration;

            LoadPage();
        }

        private void LoadPage()
        {
            InitializeComponent();

            this.editPageToolStripMenuItem.Enabled = false;

            if (Utility.SetupSettings("config.xml", ref config, ftpConnection))
            {
                this.btnListPages.Enabled = false;
            }
        }

        private void fTPSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FTPSettingsForm settingsForm = new FTPSettingsForm(ref config);

            DialogResult dialogResult = settingsForm.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                MessageBox.Show("Settings Saved");
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }

        private void picturePageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(runPicturePage));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();

            this.Close();
        }

        //run the accounting conversion form, eating the return value so we can multi-thread it
        private void runPicturePage()
        {
            PicturePage picturePage = new PicturePage(config);
            picturePage.ShowDialog();
        }

        private void btnListPages_Click(object sender, EventArgs e)
        {
            if (Utility.ConnectToFTP(ftpConnection, config))
            {
                string[] directory = ftpConnection.GetFiles();
                foreach (string page in directory)
                {
                    this.pageList.Items.Add(page);
                }
            }
            //FTPFile[] ftpFile = ftpConnection.GetFileInfos();
        }

        private void btnEditPages_Click(object sender, EventArgs e)
        {
            //this.pageList.SelectedItems[0];
        }
    }
}
